package cn.qdhlm.collectservice.core.component.analyse;


import cn.qdhlm.collectservice.common.Constant;
import cn.qdhlm.collectservice.common.util.CodeUtil;
import cn.qdhlm.collectservice.service.UtilityService;
import io.netty.buffer.ByteBuf;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Name: AnyAfn05
 * @Description: 查询参数（AFN=05H）
 * @Author: jiangcs
 */
@Log4j2
@Component
public class AnyAfn05 {
    @Autowired
    private UtilityService utilityService;

    public boolean execute(int fn, int sn, ByteBuf byteBuf, String terminalNo) {
        boolean executeFlag = false;

        switch (fn) {
            // F1：查询服务器1信息
            case 1:
                executeFlag = f1(sn, byteBuf, terminalNo);
                break;
            // F3：查询时间
            case 3:
                executeFlag = f3(sn, byteBuf, terminalNo);
                break;
            // F4：查询上传模式
            case 4:
                executeFlag = f4(sn, byteBuf, terminalNo);
                break;
            // F5：查询采集模式
            case 5:
                executeFlag = f5(sn, byteBuf, terminalNo);
                break;
            // F6：查询表信息
            case 6:
                executeFlag = f6(sn, byteBuf, terminalNo);
                break;
            // F7：查询信号
            case 7:
                executeFlag = f7(sn, byteBuf, terminalNo);
                break;
            // F8：查询ICCID
            case 8:
                executeFlag = f8(sn, byteBuf, terminalNo);
                break;
            // F9：查询程序版本
            case 9:
                executeFlag = f9(sn, byteBuf, terminalNo);
                break;
            // F255：查询全部信息2
            case 255:
                executeFlag = f255(sn, byteBuf, terminalNo);
                break;
            default:
                log.error("execute: fn[{}] match fail", fn);
        }
        return executeFlag;
    }

    private boolean f1(int sn, ByteBuf byteBuf, String terminalNo) {
        // ip或域名
        StringBuilder sbIp = new StringBuilder();
        for (int i = 0; i < Constant.BYTE_LENGTH_IP; i++) {
            String asciiStr = CodeUtil.byteToHexString(byteBuf.readByte());
            if (Constant.SYMBOL_00.equals(asciiStr)) {
                // 跳过剩余字节
                byteBuf.skipBytes(Constant.BYTE_LENGTH_IP - i - 1);
                break;
            }
            sbIp.append(CodeUtil.hexStringToAsciiString(asciiStr));
        }
        // 端口
        int port = byteBuf.readUnsignedShort();

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("ip", sbIp);
        resultMap.put("port", port);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f3(int sn, ByteBuf byteBuf, String terminalNo) {
        // 模组时间
        String second = CodeUtil.byteToHexString(byteBuf.readByte());
        String minute = CodeUtil.byteToHexString(byteBuf.readByte());
        String hour = CodeUtil.byteToHexString(byteBuf.readByte());
        String day = CodeUtil.byteToHexString(byteBuf.readByte());
        String month = CodeUtil.byteToHexString(byteBuf.readByte());
        String year = "20" + CodeUtil.byteToHexString(byteBuf.readByte());
        String moduleTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("moduleTime", moduleTime);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return false;
    }

    private boolean f4(int sn, ByteBuf byteBuf, String terminalNo) {
        // 上传模式：0：采后传  1：时段传
        int mode = byteBuf.readByte();
        String startTime = null, endTime = null;
        // 时间段上传
        if (mode == 1) {
            // 开始、结束时间
            startTime = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
            endTime = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
        }

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(4);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("uploadMode", mode);
        resultMap.put("startTime", startTime);
        resultMap.put("endTime", endTime);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f5(int sn, ByteBuf byteBuf, String terminalNo) {
        // 采集模式：0：周期  1：定时
        int mode = byteBuf.readByte();
        // 采集间隔
        int interval = byteBuf.readShortLE();
        // 定时时间1
        String time1 = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
        // 定时时间2
        String time2 = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(5);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("collectMode", mode);
        resultMap.put("interval", interval);
        resultMap.put("time1", time1);
        resultMap.put("time2", time2);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f6(int sn, ByteBuf byteBuf, String terminalNo) {
        // 客户协议编码0-255
        int codeCustomNB = byteBuf.readByte();
        String macAddr = CodeUtil.intToHexString(byteBuf.readInt());

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(3);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("codeCustomNB", codeCustomNB);
        resultMap.put("macAddr", macAddr);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f7(int sn, ByteBuf byteBuf, String terminalNo) {
        // 信号
        int signal = byteBuf.readByte();

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("signal", signal);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f8(int sn, ByteBuf byteBuf, String terminalNo) {
        // iccid
        StringBuilder sbIccid = new StringBuilder();
        for (int i = 0; i < Constant.BYTE_LENGTH_ICCID; i++) {
            String asciiStr = CodeUtil.byteToHexString(byteBuf.readByte());
            if (Constant.SYMBOL_00.equals(asciiStr)) {
                // 跳过剩余字节
                byteBuf.skipBytes(Constant.BYTE_LENGTH_ICCID - i - 1);
                break;
            }
            sbIccid.append(CodeUtil.hexStringToAsciiString(asciiStr));
        }

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("iccid", sbIccid);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f9(int sn, ByteBuf byteBuf, String terminalNo) {
        // 程序版本
        int version = byteBuf.readByte();

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(2);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("version", version);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }

    private boolean f255(int sn, ByteBuf byteBuf, String terminalNo) {
        // 客户协议编码0-255
        int codeCustomNB = byteBuf.readByte();
        String macAddr = CodeUtil.intToHexString(byteBuf.readInt());

        // 上传模式：0：采后传  1：时段传
        int uploadMode = byteBuf.readByte();
        String startTime = null, endTime = null;
        // 时间段上传
        if (uploadMode == 1) {
            // 开始、结束时间
            startTime = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
            endTime = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
        }

        // 采集模式：0：周期  1：定时
        int collectMode = byteBuf.readByte();
        // 采集间隔
        int interval = byteBuf.readShortLE();
        // 定时时间1
        String time1 = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());
        // 定时时间2
        String time2 = CodeUtil.byteToHexString(byteBuf.readByte()) + ":" + CodeUtil.byteToHexString(byteBuf.readByte());

        // ip或域名
        StringBuilder sbIp = new StringBuilder();
        for (int i = 0; i < Constant.BYTE_LENGTH_IP; i++) {
            String asciiStr = CodeUtil.byteToHexString(byteBuf.readByte());
            if (Constant.SYMBOL_00.equals(asciiStr)) {
                // 跳过剩余字节
                byteBuf.skipBytes(Constant.BYTE_LENGTH_IP - i - 1);
                break;
            }
            sbIp.append(CodeUtil.hexStringToAsciiString(asciiStr));
        }
        // 端口
        int port = byteBuf.readUnsignedShort();

        // 更新命令结果状态
        Map<String, Object> resultMap = new HashMap<>(15);
        resultMap.put("statusCode", Constant.SYMBOL_00);
        resultMap.put("codeCustomNB", codeCustomNB);
        resultMap.put("macAddr", macAddr);
        resultMap.put("uploadMode", uploadMode);
        resultMap.put("startTime", startTime);
        resultMap.put("endTime", endTime);
        resultMap.put("collectMode", collectMode);
        resultMap.put("interval", interval);
        resultMap.put("time1", time1);
        resultMap.put("time2", time2);
        resultMap.put("ip", sbIp);
        resultMap.put("port", port);
//        utilityService.updateCommandResultAndStatus(terminalNo, sn, resultMap, 2);

        return true;
    }
}
